package com.slg.android.manyanime.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.bitmap.BitmapDisplayConfig;
import com.lidroid.xutils.bitmap.callback.BitmapLoadCallBack;
import com.lidroid.xutils.bitmap.callback.BitmapLoadFrom;
import com.slg.android.manyanime.R;
import com.slg.android.manyanime.bean.Animation;
import com.slg.android.manyanime.utils.DataStore;
import com.slg.android.manyanime.view.photoview.PhotoView;

import java.util.List;

/**
 * Created by Thinkpad on 2015/3/14.
 */
public class CommonPageAdapter extends PagerAdapter {

    private List<String> data;

    private Context context;

    private String cartName;

    private String huashu;


    private LayoutInflater inflater;

    public CommonPageAdapter(Context context, List<String> data, String cartName, String huashu) {
        this.data = data;
        this.context = context;
        this.cartName = cartName;
        this.huashu = huashu;
        inflater = LayoutInflater.from(context);
    }

    private BitmapUtils bitmapUtils;


    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {

        return view == object;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        View imgLayout = inflater.inflate(R.layout.item_read_cartoon_img, container, false);
        final PhotoView imageViewcon = (PhotoView) imgLayout.findViewById(R.id.read_cartoon_img);
        final String imageUrl = data.get(position);
        final BitmapUtils bitmapUtils1 = new BitmapUtils(context);
        //bitmapUtils1.configDefaultLoadingImage(R.drawable.cartoon_hot_loading);
        //bitmapUtils1.configDefaultLoadingImage(R.drawable.face_1);
        bitmapUtils1.configDefaultLoadFailedImage(R.drawable.face_1);
        final RelativeLayout loadinglayout = (RelativeLayout) imgLayout.findViewById(R.id.common_loading);
        loadinglayout.setBackgroundColor(context.getResources().getColor(R.color.black));
        loadinglayout.setVisibility(View.VISIBLE);
        ImageView loading = (ImageView) imgLayout.findViewById(R.id.common_loading_image);
        loading.startAnimation(AnimationUtils.loadAnimation(context, R.anim.anim_common_rotate));

        bitmapUtils1.display(imageViewcon, imageUrl, new BitmapLoadCallBack<ImageView>() {
            @Override
            public void onLoadCompleted(ImageView imageView, String s, Bitmap bitmap, BitmapDisplayConfig bitmapDisplayConfig, BitmapLoadFrom bitmapLoadFrom) {
                imageViewcon.setImageBitmap(bitmap);
                imageView.setImageBitmap(bitmap);
                loadinglayout.setVisibility(View.GONE);
            }

            @Override
            public void onLoadFailed(ImageView imageView, String s, Drawable drawable) {
                Toast.makeText(context, R.string.no_network, Toast.LENGTH_SHORT).show();
                if (loadinglayout != null) {
                    loadinglayout.setVisibility(View.GONE);
                }

            }
        });


        TextView textView = (TextView) imgLayout.findViewById(R.id.read_cartoon_information_bottom);
        textView.setText(this.cartName + " 第" + huashu + "话" + " " + (position + 1) + "/" + data.size());
        ((ViewPager) container).addView(imgLayout);
        return imgLayout;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        if (container != null && object != null) {
            container.removeView((View) object);

        }
    }


}
